package llama.view;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import llama.core.Contact;
import llama.core.ContactList;
import llama.core.IUser;
import llama.core.LoginSettings;
import llama.core.SearchResults;
import llama.network.NetworkMessage;
import llama.util.SystemMessage;
import llama.view.panels.ContactInformationDialog;
import llama.view.panels.ContactListPanel;
import llama.view.panels.LoginPanel;
import llama.view.panels.SearchLogoutPanel;
import llama.view.panels.SearchUserPanel;

@SuppressWarnings("serial")
public class ContactFrame extends JFrame implements PropertyChangeListener {
	private JPanel contentPane;
	
	private ContactListPanel contactListPanel;
	private SearchUserPanel searchUserPanel;
	private LoginPanel loginPanel;
	private SearchLogoutPanel searchLogoutPanel;
	
	/**
	 * Create the frame.
	 */
	public ContactFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 250, 500);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		setVisible(true);
		
		//Initiate the panels of this frame
		this.loginPanel = new LoginPanel();
		this.contactListPanel = new ContactListPanel();
		this.searchLogoutPanel = new SearchLogoutPanel();
		this.searchUserPanel = new SearchUserPanel();
		
		viewLogin();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if((evt.getNewValue()!=null) && evt.getNewValue() instanceof NetworkMessage) {
			NetworkMessage nMsg = (NetworkMessage) evt.getNewValue();
			switch (nMsg.getType()) {
			case LOGIN_SUCCESS:
				contactListPanel.setActiveUser((IUser)nMsg.getObject());
				viewContacts();
				break;
			case LOGIN_FAIL:
				loginPanel.loginFailed((String)nMsg.getObject());
				break;
			case SEARCH_RESULTS:
				if(nMsg.getObject() instanceof SearchResults){
					searchUserPanel.showSearchResuluts((SearchResults)nMsg.getObject());
				}
				break;
			case NEW_CONTACT_LIST:
				System.out.println("CONTACTFRAM - NEW_CONTACT_LIST");
				contactListPanel.processContacts((ContactList) nMsg.getObject());
				break;
			case DISCONNECTED:
				loginPanel.logoutVisuals();
				viewLogin();
				break;
			}
		} else if((evt.getNewValue()!=null) && evt.getNewValue() instanceof SystemMessage) {
			SystemMessage sMsg = (SystemMessage) evt.getNewValue();
			switch (sMsg.getType()) {
			case NETWORK_UNAVAILABLE:
				loginPanel.loginFailed((String)sMsg.getObject());
				break;
			case SHOW_SEARCH_WINDOW:
				searchUserPanel.clear();
				viewSearch();
				break;
			case BACK_TO_CONTACT_LIST:
				viewContacts();
				break;
			case UPDATE_LOGIN_SETTINGS:
				loginPanel.updateLoginSettings((LoginSettings)sMsg.getObject());
				break;
			case SHOW_CONTACT_INFO:
				new ContactInformationDialog((Contact) sMsg.getObject());
				break;
			case LOGOUT:
				loginPanel.logoutVisuals();
				viewLogin();
				break;
			}
		}
	}
	
	private void viewSearch() {
		contentPane.removeAll();
		contentPane.add(searchUserPanel, BorderLayout.CENTER);
		contentPane.revalidate();
		contentPane.repaint();
	}
	
	private void viewContacts() {
		contentPane.removeAll();
		contentPane.add(contactListPanel, BorderLayout.CENTER);
		contentPane.add(searchLogoutPanel, BorderLayout.SOUTH);
		contentPane.revalidate();
		contentPane.repaint();
	}
	
	private void viewLogin() {
		contentPane.removeAll();
		contentPane.add(loginPanel, BorderLayout.CENTER);
		contentPane.revalidate();
		contentPane.repaint();
	}
	
}
